package sg.edu.nus.iss.pacman.form;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;

import sg.edu.nus.iss.pacman.midlet.PacManMIDlet;

/**
 * Form for user to input telephone number
 * 
 * @author Daniel
 *
 */
public class SendChallengeViaSMSForm extends TextBox implements CommandListener {

	private static final String TITLE = "Send Challenge via SMS To :";
	
	private PacManMIDlet mainApp;
	
	private Command cmdBack;
	private Command cmdSend;	
	private String telpNo;
	
	/**
	 * Constructor for this screen
	 * @param mainApp PacManMIDlet
	 */
	public SendChallengeViaSMSForm(PacManMIDlet mainApp) {
		super(TITLE, "", 15, TextField.PHONENUMBER);
		this.mainApp = mainApp;
		init();
	}
	
	/**
	 * Intializes the screen
	 */
	private void init() {
		cmdSend = new Command(PacManMIDlet.LABEL_CMD_SEND, Command.OK, 1);
		cmdBack = new Command(PacManMIDlet.LABEL_CMD_BACK, Command.BACK, 1);
		
		addCommand(cmdSend);
		addCommand(cmdBack);
		setCommandListener(this);
	}
	
	/**
	 * Validates telephone number
	 * 
	 * @return TRUE if telephone number is in correct format
	 */
	private boolean validateValues() {
		
		boolean result = false;
		telpNo = getString();
		
		if (telpNo != null && !telpNo.equals("")) {
			result = true;
		}
		
		return result;
	}
	
	/**
	 * This is a method which handles command actions
	 */
	public void commandAction(Command cmd, Displayable disp) {		
		if (cmd == cmdBack) {
			mainApp.displaySendChallengeChoiceList();
		} else if (cmd == cmdSend) {
			
			if (validateValues()) {
				mainApp.sendChallengeViaSMS(telpNo);	
			} else {
				mainApp.displayAlertError("Please enter the telephone number");
			}
		} 
	}

}
